$(function(){
    $('#movieTime').datetimepicker({
        minView:'year',
	    lang:'ch',
	    timepicker:true,
	   // format:'Y-m-d H:i:s',
	    format:'Y-m-d',
	    allowBlank: true,
	    
	});  
	$('#movieCategoryId').select2({minimumResultsForSearch : 'Infinity',theme : 'bootstrap'});
	$('#isHotSearch').select2({minimumResultsForSearch : 'Infinity',theme : 'bootstrap'});
	$('#hallId').select2({minimumResultsForSearch : 'Infinity',theme : 'bootstrap'});
	$('#isHotName').select2({minimumResultsForSearch : 'Infinity',theme : 'bootstrap'});
 
	var p ={p:null}
	selectAllMovieInfo(p);
	function selectAllMovieInfo(p){
		  $('#movie-table').empty();
		  $('#movie-table').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td><td>电影名称</td><td>上映时间</td><td>价格(元)</td><td>是否热门</td></tr>');
	$.ajax({
		  type:"POST",
		  url:"/movie.front/movieInfo/getSysAllMovieInfoList",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
           array = data.result;
           for(var i=0;i<array.length;i++){
        	   var html = '' ;
        	   var movieTime = transfer(array[i].movieTime);
        	  if(array[i].isHot==1){
        		  html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].movieName+'</td><td>'+movieTime+'</td><td>'+array[i].moviePrice+'</td><td>是</td></tr>';  
        	  }else{
        		  html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].movieName+'</td><td>'+movieTime+'</td><td>'+array[i].moviePrice+'</td><td>否</td></tr>';
        	  }
        	  
           	    $('#movie-table').append(html); 
           }
		   
          }
	
	   });
		
	}
	function transfer(val){
		if(val){
			val = parseInt(val, 10) * 1000;
			var date = new Date(val);
			//return date.format('yyyy-MM-dd hh:mm:ss');
			return date.format('yyyy-MM-dd');
		}
		return '';
	}
	//重写javaScript日期对象原型
	Date.prototype.format = function(fmt){
		var o = {
			'M+': this.getMonth() + 1,//月份
			'd+': this.getDate(),//日
			'h+': this.getHours(),//小时
			'm+': this.getMinutes(),//分
			's+': this.getSeconds(),//秒
			'q+': Math.floor((this.getMonth() + 3) / 3),//季度
			'S': this.getMilliseconds()//毫秒
		};
		if(/(y+)/.test(fmt))
			fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
		for( var k in o)
			if(new RegExp('(' + k + ')').test(fmt))
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
		return fmt;
	}
	
	$('#btn-search').click(function(){
		var movieName = $('#search-movieName')[0].value;
	    var isHot =$('#isHotSearch')[0].value;
	    var p = {movieName:movieName,isHot:isHot};
	    selectAllMovieInfo(p);
	});
	
	//重置
	$('#btn-reset').click(function(){
		$('#search-movieName').val("");
		$("#isHotSearch").val(null).trigger('change');
	
	})

   $('#btn-insert').click(function(){
	   emptyMyDialog();
	  $('#myModal').modal('show');
     });
	selectAllHall();
	//影厅
	function selectAllHall(){
		var p={p:null}
		$.ajax({
			  type:"POST",
			  url:"/movie.front/movieInfo/getSysfindAllHall",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
					$('#hallId').select2({
						theme : 'bootstrap',
						data : data.result
					});
	   
			   
	          }
		
		   });
	};
	selectAllCategory();
	//种类
	function selectAllCategory(){
		var p={p:null}
		$.ajax({
			  type:"POST",
			  url:"/movie.front/movieInfo/findAllCategory",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
	     	$('#movieCategoryId').select2({
				theme : 'bootstrap',
				data : data.result
			});
	          }
		
		   });
	}
	//清空对话框内容
	function emptyMyDialog(){
		$('#movieName')[0].value='';
		$('#movieTime')[0].value='';
		$('#moviePrice')[0].value='';
	//	$('#moviePicture')[0].value='';
		$('#movieIntro')[0].value='';
		$('#id')[0].value='';
		$("#hallId").val(null).trigger('change');
		$("#movieCategoryId").val(null).trigger('change');
		$("#isHot").val(null).trigger('change');
		
	}
	
	//保存图片
	$('#btn-save-movie').click(function(){
		debugger;
	var movieName =	$('#movieName')[0].value;
	var movieTime =	$('#movieTime')[0].value;
	var moviePrice =$('#moviePrice')[0].value;
	var movieIntro =	$('#movieIntro')[0].value;
	var hallId =	$("#hallId")[0].value;
	var movieCategoryId =	$("#movieCategoryId")[0].value;
	var ishot =	$('#isHotName')[0].value;;
	var id = $('#id')[0].value;
	if(id==null || id==""){
	var p={movieName:movieName,movieTime:movieTime,moviePrice:moviePrice,movieIntro:movieIntro,hallId:hallId,movieCategoryId:movieCategoryId,isHot:ishot}
	$.ajax({
		  type:"POST",
		  url:"/movie.front/movieInfo/addMovieInfo",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
			 if(data.successcode>0){
			  $('#myModal').modal('hide');
			  var d={d:null}
           	selectAllMovieInfo(d);
			  emptyMyDialog();
			 }else{
				 alert("网络连接失败");
			 }
        }
	
	   });
	}else{
		var p={id:id,movieName:movieName,movieTime:movieTime,moviePrice:moviePrice,movieIntro:movieIntro,hallId:hallId,movieCategoryId:movieCategoryId,isHot:ishot}
		$.ajax({
			  type:"POST",
			  url:"/movie.front/movieInfo/updateMovieInfo",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				//  alert(data.successcode);
				  if(data.successcode!=null){
					  $('#myModal').modal('hide');
						//刷新表格
			            	var d={d:null}
			            	selectAllMovieInfo(d);					  
				  }
				  if(data.errorcode!=null){
		            //	alert(data.errorcode);
		             alert("网络连接失败");  
				  }

			   
			  }
		
		   });
	}
	
	});
	
	//修改电影信息
	$('#btn-update-movie').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  $('#myModal').modal('show');
			  var id = {id:id};
			  getByIdMovieInfo(id);
		     }
	});
	
	function getByIdMovieInfo(p){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/movieInfo/getMovieInfoById",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				   	$('#movieName')[0].value=data.result.movieName;
					$('#movieTime')[0].value=transfer(data.result.movieTime);
					$('#moviePrice')[0].value=data.result.moviePrice;
					$('#movieIntro')[0].value=data.result.movieIntro;
				    $("#hallId").val(data.result.hallId).trigger('change');
					$("#movieCategoryId").val(data.result.movieCategoryId).trigger('change');
					$("#isHotName").val(data.result.isHot).trigger('change');
			        $('#id')[0].value=data.result.id;
			  }
		
		   });
	}
	
	//删除电影信息
	$('#btn-remove-movie').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  var id = {id:id};
			  deleteMovieById(id);
		     }
		
	});
	
	function deleteMovieById(id){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/movieInfo/deleteMovieInfoById",
			  data:JSON.stringify(id),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  if(data.successcode!=null){
					  $('#myModal').modal('hide');
						//刷新表格
			            	var d={d:null}
			            	selectAllMovieInfo(d);	
			            	alert("删除成功");
				  }
				  if(data.errorcode!=null){
		             alert("网络连接失败");  
				  }
			  }
		
		   });
	}
	
	//上传影片的信息
	$('#btn-upload-picture').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  
			  $('#myPictureModal').modal('show');
			  $('#idFile')[0].value=id;
			 
		     }
		
	});
	
	$('#btn-save-picture').click(function(){
		var id = $('#idFile')[0].value;
		
		uploadMoviePicture(id);
	});
	
	function uploadMoviePicture(id){
		   $.ajaxFileUpload({
                   url: '/movie.front/movieInfo/uploadMovieById', //用于文件上传的服务器端请求地址
                   type: 'post',
                   data:{id:id}, //此参数非常严谨，写错一个引号都不行
                   secureuri: false, //一般设置为false
                   fileElementId: 'uploadFile', //文件上传空间的id属性  <input type="file" id="file" name="file" />
                   dataType: 'JSON',//返回值类型 一般设置为json
                   success: function (data,status){  //服务器成功响应处理函数
                	   if(status=='success'){
                           data = jQuery.parseJSON(jQuery(data).text());
                		   if(data.successcode>0){
                			   alert("上传成功");
                			   $('#myPictureModal').modal('hide');
                		   }else{
                			   alert("上传失败");
                		   }
                	   }
             
                   },
                   error: function (data)//服务器响应失败处理函数
                   {
                       alert("网络连接失败");
                   }
               })
          }
	/**添加多个文件项**/
	$('#btn-upload-person').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  
			  $('#personPicturesModal').modal('show');
			  $('#idPersonFile')[0].value=id;
			 
		     }
		
	});
	
	$('#btn-save-person-picture').click(function(){
		var id = $('#idPersonFile')[0].value;
		uploadPersonPicture(id);
	})
	
	function uploadPersonPicture(id){
		   $.ajaxFileUpload({
            url: '/movie.front/movieInfo/uploadPersonById', //用于文件上传的服务器端请求地址
            type: 'post',
            data:{id:id}, //此参数非常严谨，写错一个引号都不行
            secureuri: false, //一般设置为false
            fileElementId: 'uploadFile', //文件上传空间的id属性  <input type="file" id="file" name="file" />
            dataType: 'JSON',//返回值类型 一般设置为json
            success: function (data,status){  //服务器成功响应处理函数
         	   if(status=='success'){
                    data = jQuery.parseJSON(jQuery(data).text());
         		   if(data.successcode>0){
         			   alert("上传成功");
         			   $('#myPictureModal').modal('hide');
         		   }else{
         			   alert("上传失败");
         		   }
         	   }
      
            },
            error: function (data)//服务器响应失败处理函数
            {
                alert("网络连接失败");
            }
        });
	}
 });
